﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using RepositoryFramework.Validators;

namespace RepositoryFramework.Exceptions
{
	/// <summary>
	/// Class <see cref="CustomValidationException"/> define custom validation exception
	/// </summary>
	public class CustomValidationException : ValidationException
	{
		#region Fields
		private readonly IList<ValidationError> _errors;
		#endregion

		#region Properties
		/// <summary>
		/// Gets a message that describes the current exception.
		/// </summary>
		/// <returns>The error message that explains the reason for the exception, or an empty string("").</returns>
		public override string Message
		{
			get
			{
				if (_errors != null)
				{
					var result = new StringBuilder();
					_errors.Select(e => result.AppendLine(e.Message));
					return result.ToString();
				}
				return base.Message;
			}
		}
		#endregion

		#region .ctor
		/// <summary>
		/// Initializes a new instance of the <see cref="CustomValidationException"/> class.
		/// </summary>
		/// <param name="errors">The list.</param>
		public CustomValidationException(IList<ValidationError> errors)
		{
			_errors = errors;
		}
		#endregion


	}
}